//
//  Module2TableViewCell.m
//  huien
//
//  Created by yiban on 2017/5/26.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "Module2TableViewCell.h"
#import "UIButton+YBAdd.h"

#import "ModuleModel.h"
//// 宽度(自定义)
//#define PIC_WIDTH 40
//// 高度(自定义)
//#define PIC_HEIGHT 40
// 列数(自定义)
#define COL_COUNT 3
@interface Module2TableViewCell()
{
    
}
@property(nonatomic,strong) NSMutableArray *moudleArray;
@end
@implementation Module2TableViewCell
@synthesize moudleArray=_moudleArray;

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!_moudleArray) {
            _moudleArray =[[NSMutableArray alloc] init];
        }
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
}
/** 九宫格形式添加图片 */
- (void)addPictures {
    for (int i = 0; i < self.moudleArray.count; i++) {
        ModuleModel *moudleModel = self.moudleArray[i];
        // 图片所在行
        NSInteger row = i / COL_COUNT;
        // 图片所在列
        NSInteger col = i % COL_COUNT;
        
        UIView *mView =[[UIView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH/3)*col, row *(170/2), SCREEN_WIDTH/3, 170/2)];
        mView.backgroundColor=[UIColor whiteColor];
        mView.userInteractionEnabled = YES;
        mView.tag = i+1;
        NSString *imagename = [NSString stringWithFormat:@"icon-0%ld",(long)moudleModel.Id];
        UIImage *tempImg =[UIImage imageNamed:imagename];
        CGFloat PIC_WIDTH =tempImg.size.width;
        CGFloat PIC_HEIGHT =tempImg.size.height;
        //创建图片
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((mView.frame.size.width-PIC_WIDTH)/2, 10, PIC_WIDTH, PIC_HEIGHT)];
        imageView.image = tempImg;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [mView addSubview:imageView];
        
        UILabel *LabelV = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frame.origin.y+imageView.frame.size.height+5, mView.frame.size.width, 20)];
        LabelV.text = moudleModel.title;
        LabelV.font = [UIFont boldSystemFontOfSize:12];
        LabelV.textColor =  UIColorFromRGB(0x666666);
        LabelV.textAlignment = NSTextAlignmentCenter;
        LabelV.backgroundColor = [UIColor clearColor];
        LabelV.lineBreakMode = NSLineBreakByClipping;
        LabelV.numberOfLines = 1;
        [mView addSubview:LabelV];
        LabelV=nil;
        imageView=nil;
        [self addSubview:mView];
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickScrollView:)];
        [mView addGestureRecognizer:gesture];
        gesture = nil;
        mView=nil;
    }
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    self.data =model;
    self.indexPath=indexPath;
    self.moudleArray =(NSMutableArray *)model;
    [self addPictures];
}
+(CGFloat)returnCellHeight
{
    return 170;
}
-(void)dealloc
{
    [LcbLog log:@"ModuleTableViewCell--------->dealloc"];
}
- (void)clickScrollView:(UITapGestureRecognizer *)gesture {
    
    if (self.actionBlock) {
        self.actionBlock(BEventType_Module,self,[gesture view].tag,self.data,self.indexPath);
    }
}

@end
